/**
 * Copyright (c) 2012 Axa Holding Belgium, SA. All rights reserved.
 * This software is the confidential and proprietary information of the AXA Group.
 */
package chabernac.android.wordbattle.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Word {
  private final List<Letter> myLetters;
  private final boolean isLettersOnLine;
  private final List<Letter> myEndPointLetters = new ArrayList<Letter>();

  public Word( List<Letter> aLetters ) {
    super();
    myLetters = Collections.unmodifiableList(new ArrayList<Letter>( aLetters ));
    isLettersOnLine = determineLettersOnLine();
    determineEndPointLetters();
  }
  
  public boolean contains(Letter aLetter)
  {
	  return myLetters.contains(aLetter);
  }
  
  public Letter getNextSelectedLetter(Letter aLetter)
  {
	  Letter aResultLetter = null;
	  int index = myLetters.lastIndexOf(aLetter);
	  if ( ( index > -1 ) && 
		   ( index < (myLetters.size()-1) ) )
	  {
		  aResultLetter = myLetters.get(index++);
	  }
	  return aResultLetter;
  }
  
  public Letter getPreviousSelectedLetter(Letter aLetter)
  {
	  Letter aResultLetter = null;
	  int index = myLetters.lastIndexOf(aLetter);
	  if ( ( index > 0 ) && 
		   ( index < myLetters.size() ) )
	  {
		  aResultLetter = myLetters.get(index--);
	  }
	  return aResultLetter;
  }

  public List<Letter> getLetters() {
    return Collections.unmodifiableList( myLetters );
  }

  public boolean isLettersOnLine() {
    return isLettersOnLine;
  }
  
  public List<Letter> getEndPointLetters() {
    return Collections.unmodifiableList( myEndPointLetters );
  }

  private boolean determineLettersOnLine(){
    if(myLetters.size() >= 2){
      int theDifColumn = myLetters.get(1).getColumn() - myLetters.get(0).getColumn();
      int theDifRow = myLetters.get(1).getRow() - myLetters.get(0).getRow();

      for(int i=2;i<myLetters.size();i++){
        int theDColumn = myLetters.get(i).getColumn() - myLetters.get(i-1).getColumn();
        if(theDColumn != theDifColumn) return false;
        int theDRow = myLetters.get(i).getRow() - myLetters.get(i-1).getRow();
        if(theDRow != theDifRow) return false;
      }

      return true;
    }
    return false;
  }

  private void determineEndPointLetters(){
    if(myLetters.size() < 2) return;
    Letter thePreviousLetter = null;
    int difx = 0;
    int dify = 0;
    boolean isDifSet = false;
    for(Letter theLetter : myLetters){
      if(thePreviousLetter == null){
        myEndPointLetters.add(theLetter);
      } else {
        int theDifx = theLetter.getColumn() - thePreviousLetter.getColumn();
        int theDify = theLetter.getRow() - thePreviousLetter.getRow();

        if(isDifSet && ( theDifx != difx || theDify != dify)){
          myEndPointLetters.add(thePreviousLetter);
        }
        difx = theDifx;
        dify = theDify;
        isDifSet = true;
      }
      thePreviousLetter = theLetter;
    }
    myEndPointLetters.add(myLetters.get(myLetters.size() - 1));
  }
  
  public String toString(){
    String theString = "";
    for(Letter theLetter : myLetters){
      theString += theLetter.getChar();
    }
    return theString;
  }
  
  public boolean equals(Object anObject){
    if(!(anObject instanceof Word)) return false;
    return toString().equals(anObject.toString());
  }
  
  public int hashCode(){
    return toString().hashCode();
  }
  
  public Letter getLastLetter(){
    return myLetters.get( myLetters.size() - 1 );
  }
}
